﻿using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace GameEditor
{
    class ImportFile
    {
        ContentBuilder contentBuilder;

        public ImportFile(ContentBuilder cBuilder, ContentType type)
        {
            contentBuilder = cBuilder;

            switch (type)
            {
                case ContentType.Model:
                    //ImportModel();
                    break;
                case ContentType.Texture:
                    ImportTexture();
                    break;
                default:
                    break;
            }
        }

        private void ImportTexture()
        {
            OpenFileDialog fileDialog = new OpenFileDialog();

            // Default to the directory which contains our content files.
            //set default location in settings instead
            string assemblyLocation = Assembly.GetExecutingAssembly().Location;
            string relativePath = Path.Combine(assemblyLocation, "../../../../Content");
            string contentPath = Path.GetFullPath(relativePath);

            fileDialog.InitialDirectory = contentPath;

            fileDialog.Title = "Import Texture";

            fileDialog.Filter = "Texture Files (*.jpg;*.bmp;*.png)|*.jpg;*.bmp;*.png|" +
                                "JPG Files (*.jpg)|*.jpg|" +
                                "BMP Files (*.bmp)|*.bmp|" +
                                "PNG Files (*.png)|*.png|" +
                                "All Files (*.*)|*.*";

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                LoadTexture(fileDialog.FileName);
            }
        }

        /// <summary>
        /// Loads a new 2D texture file into...some view control...
        /// </summary>
        private void LoadTexture(string fileName)
        {
            Cursor.Current = Cursors.WaitCursor;

            //original code unloads current model, thats pointless... we want to use ALL THE TEXTURES!

            //use the provided contentbuilder
            contentBuilder.Clear();
            contentBuilder.Add(fileName, fileName, null, "TextureProcessor");

            // Build the texture stuff
            string buildError = contentBuilder.Build();

            if (string.IsNullOrEmpty(buildError))
            {
                // If the build succeeded, use the ContentManager to
                // load the temporary .xnb file that we just created.
                //modelViewerControl.Texture = contentManager.Load<Texture2D>("Texture");
            }
            else
            {
                // If the build failed, display an error message.
                MessageBox.Show(buildError, "Error");
            }

            Cursor.Current = Cursors.Arrow;
        }
    }
}
